import {
  getTeamList,
  auditing,
  memberDel,
  teamDel,
  teamQuit
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    curTab: 1,
    teamId: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    debugger;
    this.setData({
      teamId: options.team_id ? options.team_id : "",
    })


  },
  onShow: function() {
    let {
      curTab,
      teamId
    } = this.data;
    let self = this;
    getTeamList(teamId, curTab, (r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data,
        })
      }
    })
  },
  //删除团队成员
  onDel: function(e) {
    let {
      curTab,
      teamId
    } = this.data;
    let self = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '删除成员',
      content: '确定要删除该成员吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          memberDel(id, (r) => {
            if (r.code == 0) {
              getTeamList(teamId, curTab, (r) => {
                if (r.code == 0) {
                  self.setData({
                    dataInfo: r.data,
                  })
                  errorMsg("删除成功");
                } else {
                  errorMsg(r.message)
                }
              })
            } else {
              errorMsg(r.message)
            }
          })
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })


  },
  //操作审核 同意或者拒绝团队加入
  onCaoZuo: function(e) {
    let self = this;
    let id = e.currentTarget.dataset.id;
    let {
      teamId,
      curTab
    } = this.data;
    wx.showModal({
      title: '审核成员',
      content: '是否同意该成员加入团队。',
      showCancel: true, //是否显示取消按钮
      cancelText: "拒绝", //默认是“取消”
      cancelColor: '#ff6a6a', //取消文字的颜色
      confirmText: "同意", //默认是“确定”
      confirmColor: '#46d5a4', //确定文字的颜色
      success: function(res) {
        if (res.cancel) {
          //点击拒绝,默认隐藏弹框
          auditing(2, id, teamId, (r) => {
            if (r.code == 0) {
              getTeamList(teamId, curTab, (r) => {
                if (r.code == 0) {
                  self.setData({
                    dataInfo: r.data,
                  })
                } else {
                  errorMsg(r.message)
                }
              })
            } else {
              errorMsg(r.message)
            }
          })
        } else {
          //点击同意
          auditing(1, id, teamId, (r) => {
            if (r.code == 0) {
              getTeamList(teamId, curTab, (r) => {
                if (r.code == 0) {
                  self.setData({
                    dataInfo: r.data,
                  })
                } else {
                  errorMsg(r.message)
                }
              })
            } else {
              errorMsg(r.message)
            }
          })
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //退出团队
  teamQuit: function() {
    wx.showModal({
      title: '退出团队',
      content: '确定要退出该团队吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          teamQuit((r) => {
            if (r.code == 0) {
              wx.navigateBack();
              setTimeout(function () {
                errorMsg(r.message)
              }, 500)
             
            } else {
              errorMsg(r.message)
            }
          })
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //解散团队
  teamDel: function() {
    wx.showModal({
      title: '解散团队',
      content: '确定要解散该团队吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          teamDel((r) => {
            if (r.code == 0) {
              wx.navigateBack();
              setTimeout(function(){
                errorMsg('解散成功')
              },400)
             
            } else {
              errorMsg(r.message)
            }
          })
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //切换tab
  changeTab: function(e) {
    let self = this;
    let {
      teamId
    } = this.data;
    getTeamList(teamId, e.currentTarget.dataset.index, (r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data,
          curTab: e.currentTarget.dataset.index
        })
      }
    })
  },

  //创建团队
  onCreate: function() {
    wx.navigateTo({
      url: '../teamcreate/teamcreate',
    })
  },
  //跳转到邀请团队成员
  invSubUser: function() {
    wx.navigateTo({
      url: '../invsubuser/invsubuser?team_id=' + this.data.teamId,
    })
  }
})